<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addDialog = true">添加分类</el-button>
      </div>

      <!-- 表格 -->

      <el-table :data="ArtCateList" style="width: 100%" border>
        <el-table-column  label="序号" width="50" type="index">
        </el-table-column>
        <el-table-column prop="cate_name" label="分类名称" >
        </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作" width="200"> 
          <template #default="{row}">
            <el-button type="primary" size="mini" @click="getArtCateDetail(row.id)">修改</el-button>
            <el-button type="danger" size="mini" @click="delArtCate(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加的对话弹框 -->
      <el-dialog
      @close="$refs.cateFormRef.resetFields()"
      title="添加文章分类"
      :visible.sync="addDialog"
      width="35%"
      >

      <el-form label-width="80px" :model="addCateForm" :rules="FormRules" ref="cateFormRef">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input  v-model="addCateForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input  v-model="addCateForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialog = false">取 消</el-button>
        <el-button type="primary" @click="addArt">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改的对话弹框 -->
    <el-dialog
      title="添加文章分类"
      :visible.sync="editDialog"
      width="35%"
      >
      
      <el-form label-width="80px" :model="editCateForm" :rules="FormRules" ref="queFormRef">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input  v-model="editCateForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input  v-model="editCateForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialog = false">取 消</el-button>
        <el-button type="primary" @click="editArtCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';
export default {
  name: "ArtCate",
  data() {
    return {
      editCateForm:{
        cate_name:'',
        cate_alias:'',
      },
      ArtCateList: [],
      addDialog:false,
      editDialog: false,
      addCateForm:{
        cate_name:'',
        cate_alias:'',
      },
      FormRules:{
        cate_name:[
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message:'非空,长度1-10',trigger: 'blur'},
        ],
        cate_alias:[
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message:'非空,长度1-10',trigger: 'blur'},
        ],
      }
    };
  },
  created() {
    this.getArtCate();
  },
  methods: {
    // 删除文章分类
    delArtCate(id){
        this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const {data:res} = await this.$axios.delete('/my/cate/del?id='+id)
          if (res.code !== 0) return this.$message.error(res.message)

          this.$message.success(res.message)

          this.getArtCate()

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    // 获取数据并且渲染数据
    async getArtCate() {
      const { data: res } = await this.$axios.get("/my/cate/list");
      // debugger
      if (res.code !== 0) return this.$message.error(res.message);
      // console.log(res);
      this.$message.success(res.message);

      this.ArtCateList = res.data;
    },
// 添加数据
 addArt(){
     this.$refs.cateFormRef.validate(async (valid)=>{
       if (!valid) return
       const {data: res} = await this.$axios.post('/my/cate/add',this.addCateForm)
    //  console.log(res)
        this.addDialog = false 
      if (res.code !== 0)  return this.$message.error(res.message)
        
      this.$message.success(res.message)

      this.getArtCate()
     })
    },

// 修改数据 点击修改
   async getArtCateDetail(id){
     const {data:res} = await this.$axios.get('/my/cate/info',{params:{id}})
    //  console.log(res,'getArtCateDetail')
    //  console.log(res);
     // 将获取到的详情的数据填充到表单
     this.editCateForm = res.data
     // 将对话框显示出来
     this.editDialog = true
   },
  //  修改用户数据 确认
   editArtCate(){
    this.$refs.queFormRef.validate(async (valid) => {
      if (!valid) return 

    const {data:res} = await this.$axios.put('/my/cate/info',this.editCateForm)

    if (res.code !== 0) return this.$message.error(res.message)

    this.$message.success(res.message)

    this.getArtCate()
    
    this.editDialog = false
    })
  }
  },
  
};
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
